package com.study;

import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 生命周期: 请求->tomcat容器->filter过滤器->servlet->inteceptor拦截器->Servic->controller
 * @author Ychiha
 * @version 1.0
 * @date 4/1/2022 下午10:23
 */
@Component
public class MyFilter extends OncePerRequestFilter {

    public static ThreadLocal<Integer> auth = new ThreadLocal<>();

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        auth.set(0);
        //从数据库中获取该权限值，如果为1则授权
        if(request.getHeader("Authorization").equals("1")){
            auth.set(1);
        }
        doFilter(request,response,filterChain);
    }
}
